﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO; 

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }






        private void tvExployer_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = tvExployer.SelectedNode;
            BindInfo(node);
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            TreeNode node1 = new TreeNode();
            node1.Text = @"C:\";
            node1.Tag = @"C:\";
            this.tvExployer.Nodes.Add(node1);
            TreeNode node2 = new TreeNode();
            node2.Text = @"D:\";
            node2.Tag = @"D:\";
            this.tvExployer.Nodes.Add(node2);

        }

        void BindInfo(TreeNode node)
        {
            #region 把该磁盘上的所有文件目录显示出来
            DirectoryInfo directoryInfo = new DirectoryInfo(node.Tag.ToString());
            DirectoryInfo[] dirs = directoryInfo.GetDirectories();
            foreach (DirectoryInfo di in dirs)
            {
                TreeNode temp = new TreeNode();
                temp.Text = di.Name;
                temp.Tag = di.FullName;
                node.Nodes.Add(temp);
            }
            #endregion

            #region 绑定本目录下的所有文件
            FileInfo[] fileInfo = directoryInfo.GetFiles();
            List<MyFile> files = new List<MyFile>();
            foreach(FileInfo myFile in fileInfo)
            {
             MyFile  file=new MyFile ();
             file.FileName = myFile.Name;
             file.FileLength = myFile.Length;
             file.FileType = myFile.Extension;
             file.FilePath = myFile.FullName;
             files.Add(file);
            }
            #endregion

            ShowFile(files);

        }

        void ShowFile(List<MyFile> files)
        {
            ListViewItem item = null;
            lvFiles.Items.Clear();
            foreach (MyFile file in files)
            {
                item = new ListViewItem();
                item.Text=file.FileName;//主键列
                item.SubItems.Add(file.FileLength.ToString());
                item.SubItems.Add(file.FileType);
                item.SubItems.Add(file.FilePath);
                this.lvFiles.Items.Add(item);
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (lvFiles.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择文件");
                return;
            }
            string path = lvFiles.SelectedItems[0].SubItems[3].Text;
            DialogResult result =
 MessageBox.Show(this,"确定要删除此项吗?","警告",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                File.Delete(path);
                MessageBox.Show("删除成功");
                this.lvFiles.SelectedItems[0].Remove();
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (lvFiles.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择文件");
                return;
            }
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult result = fbd.ShowDialog();
            string path = lvFiles.SelectedItems[0].SubItems[3].Text;
            string desPath = "";
            if (result == DialogResult.OK)
            {
                desPath = fbd.SelectedPath;
                desPath += "\\" + lvFiles.SelectedItems[0].SubItems[0].Text;
                File.Copy(path, desPath);
                MessageBox.Show("复制成功");

            }
        }
    }
}
